const { User } = require('../schemas/userSchema')
const { underSend } = require('../utils/common')

// 添加用户
const insertUser = (req, res) => {
	const account = req.body?.account
	const password = req.body?.password
	const email = req.body?.email
	const user = new User({ account, password, email })
	// 进行添加
	user.save((err, data) => {
		underSend(res, { type: 'database_insertUser', err, data })
	})
}
// 删除用户
const deleteUser = (req, res) => {
	const account = req.body?.account
	User.deleteOne({ account }, (err, data) => {
		underSend(res, { type: 'database_deleteUser', err, data })
	})
}
// 修改密码
const updateUserPassword = (req, res) => {
	const account = req.body?.account
	const password = req.body?.password
	User.updateOne({ account }, { password }, (err, data) => {
		underSend(res, { type: 'database_updateUserPassword', err, data })
	})
}
// 查找表内所有信息
const getUsers = (req, res) => {
	User.find({}, (err, data) => {
		underSend(res, { type: 'database_getUsers', err, data })
	})
}

module.exports = {
	insertUser,
	deleteUser,
	updateUserPassword,
	getUsers
}
